<template>
  <div>
   <el-card>
         <!-- 搜索区域 -->
    <div class="search_box">
      <el-form :model="searchForm" label-width="40px">
        <el-row :gutter="20">
          <el-col :span="5">
            <el-form-item label="类型">
              <el-select
                clearable
                v-model="searchForm.type"
                placeholder="请选择入库类型"
                @clear="handleSearch"
                @keyup.enter.native="handleSearch"
              >
                <el-option label="捐赠" value="1"></el-option>
                <el-option label="下拨" value="2"></el-option>
                <el-option label="采购" value="3"></el-option>
                <el-option label="借用" value="4"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="单号">
              <el-input
                @clear="handleSearch"
                @keyup.enter.native="handleSearch"
                clearable
                v-model="searchForm.inNum"
                placeholder="请输入入库单号"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="状态">
              <el-select
                clearable
                @clear="handleSearch"
                @keyup.enter.native="handleSearch"
                v-model="searchForm.status"
                placeholder="请选择入库类型"
              >
                <el-option label="已入库" :value="0"></el-option>
                <el-option label="回收站" :value="1"></el-option>
                <el-option label="待审核" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="时间">
              <el-date-picker
                style="width: 90%"
                v-model="time"
                @clear="handleSearch"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :value-format="'yyyy-MM-dd HH:mm:ss'"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="1">
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="handleSearch"
              >搜索</el-button
            >
          </el-col>
        </el-row>
      </el-form>
      <el-row>
        <el-button type="warning" icon="el-icon-refresh" @click="handleReset"
          >重置</el-button
        >
        <el-button type="success" icon="el-icon-plus"  v-permission="'inStock:in'">入库</el-button>
        <el-button icon="el-icon-download">导出</el-button>
      </el-row>
    </div>
    <!-- 表格区域 -->
    <div class="table_box">
      <el-table size="small" :data="stockList" border stripe>
        <el-table-column align="center" prop="id" label="#"> </el-table-column>
        <el-table-column
          align="center"
          prop="inNum"
          width="200"
          show-overflow-tooltip
          label="入库单号"
        >
        </el-table-column>
        <el-table-column align="center" prop="type" label="物资类型">
          <template scope="scope">
            <el-tag v-if="scope.row.type === 1">捐赠</el-tag>
            <el-tag v-if="scope.row.type === 2" type="info">下拨</el-tag>
            <el-tag v-if="scope.row.type === 3" type="success">采购</el-tag>
            <el-tag v-if="scope.row.type === 4" type="warning">借用</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="productNumber" label="数量">
        </el-table-column>
        <el-table-column align="center" prop="phone" label="联系方式">
        </el-table-column>
        <el-table-column align="center" prop="status" label="状态">
          <template scope="scope">
            <el-tag v-if="scope.row.status === 0" type="success">已入库</el-tag>
            <el-tag v-if="scope.row.status === 1" type="danger">回收站</el-tag>
            <el-tag v-if="scope.row.status === 2" type="info">待审核</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="operator" label="操作员">
        </el-table-column>
        <el-table-column align="center" prop="supplierName" label="物资提供方">
        </el-table-column>
        <el-table-column
          align="center"
          width="150"
          prop="createTime"
          label="入库时间"
        >
        </el-table-column>
        <el-table-column align="center" width="100" prop="id" label="操作">
          <template scope="scope">
            <el-button type="text" @click="getdetalis(scope.row)"  v-permission="'inStock:detail'"
              >明细</el-button
            >
            <span v-if="scope.row.status === 0">
              <el-popconfirm
                @confirm="gorecycle(scope.row.id)"
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="这是一段内容移入回收站吗？"
              >
                <el-button slot="reference" type="text"  v-permission="'inStock:remove'">回收站</el-button>
              </el-popconfirm>
            </span>
            <span v-if="scope.row.status === 1">
              <el-popconfirm
                @confirm="backStock(scope.row.id)"
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="这是一段内容移出回收站吗？"
              >
                <el-button slot="reference" type="text"  v-permission="'inStock:back'">还原</el-button>
              </el-popconfirm>
            </span>

            <span v-if="scope.row.status === 1">
              <el-popconfirm
                @confirm="delStock(scope.row.id)"
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="这是一段内容删除吗？"
              >
                <el-button type="text" slot="reference"  v-permission="'inStock:delete'">删除</el-button>
              </el-popconfirm>
            </span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <page-tools
      :total="total"
      :paginationPage="searchForm.pageNum"
      :paginationPagesize="searchForm.pageSize"
      @pageChange="pageChange"
      @pageSizeChange="pageSizeChange"
    ></page-tools>
    <!-- 入库明细 -->
    <el-dialog title="入库明细" width="65%" :visible.sync="dialogVisible">
      <el-card>
        <el-descriptions class="margin-top" :column="4">
          <el-descriptions-item label="省市区">
            <el-tag size="small">{{
              detalisData.supplierVO.address
            }}</el-tag></el-descriptions-item
          >
          <el-descriptions-item label="具体位置">
            <el-tag size="small">{{
              detalisData.supplierVO.name
            }}</el-tag></el-descriptions-item
          >
          <el-descriptions-item label="联系人">
            <el-tag size="small">{{
              detalisData.supplierVO.contact
            }}</el-tag></el-descriptions-item
          >
          <el-descriptions-item label="电话">
            <el-tag size="small">{{ detalisData.supplierVO.phone }}</el-tag>
          </el-descriptions-item>
        </el-descriptions>
      </el-card>
      <!--                步骤条-->
      <el-steps
        simple
        v-if="detalisData.status === 0"
        style="margin-left: 10px; margin-bottom: 5px"
        :space="200"
        :active="3"
        finish-status="success"
      >
        <el-step title="提交入库单"></el-step>
        <el-step title="审核入库单"></el-step>
        <el-step title="进入库存"></el-step>
      </el-steps>
      <el-steps
        simple
        v-if="detalisData.status === 2"
        style="margin-left: 10px; margin-bottom: 5px"
        :space="200"
        :active="2"
        finish-status="success"
      >
        <el-step title="提交入库单"></el-step>
        <el-step title="审核入库单"></el-step>
        <el-step title="进入库存"></el-step>
      </el-steps>
      <el-table :data="detalisData.itemVOS" border stripe>
        <el-table-column align="center" prop="name" label="名称">
        </el-table-column>
        <el-table-column align="center" prop="pnum" label="编号">
        </el-table-column>
        <el-table-column align="center" prop="pnum" label="规格">
        </el-table-column>
        <el-table-column align="center" prop="operator" label="图片">
          <template scope="scope">
            <el-image :src="scope.row.imageUrl">
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="count" label="数量">
        </el-table-column>
        <el-table-column align="center" prop="unit" label="单位">
        </el-table-column>
      </el-table>
      <!-- <el-pagination
        small
        layout="prev, pager, next,"
        :total="detalisData.total"
      >
      </el-pagination> -->
    </el-dialog>
   </el-card>
  </div>
</template>

<script>
import {
  getInstockListApi,
  getInstockDetalisApi,
  removestockApi,
  backstockApi,
  delstockApi

} from '../../../api/instock'
export default {
  name: 'instocks',
  data () {
    return {
      dialogVisible: false,
      time: '',
      searchForm: {
        pageNum: 1,
        pageSize: 5,
        type: '',
        status: 0,
        inNum: ''
      },
      total: 0,
      stockList: [],
      detalisData: {
        supplierVO: {},
        itemVOS: [],
        total: 0,
        status: 0
      }
    }
  },
  created () {
    this.getStockList()
  },
  methods: {
    // 删除
    async delStock (id) {
      const res = await delstockApi(id)
      if (!res.success) return this.$notify.error(res.data.errorMsg)
      this.$notify.success('删除成功')
      this.getStockList()
    },
    // 恢复
    async backStock (id) {
      const res = await backstockApi(id)
      if (!res.success) return this.$notify.error(res.data.errorMsg)
      this.$notify.success('恢复成功')
      this.getStockList()
    },
    // 回收站
    async gorecycle (id) {
      const res = await removestockApi(id)
      if (!res.success) return this.$notify.error(res.data.errorMsg)
      this.$notify.success('移入成功')
      this.getStockList()
    },
    // 获取明细
    getdetalis (row) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.getInfo(row.id)
      })
    },
    async getInfo (id) {
      const res = await getInstockDetalisApi(id)
      if (!res.success) return this.$notify.error(res.data.errorMsg)
      this.detalisData = res.data
    },
    // 查询
    handleSearch () {
      this.searchForm.pageNum = 1
      this.getStockList()
    },
    async getStockList () {
      const startTime = this.time[0] === '' ? '' : this.time[0]
      const endTime = this.time[1] === '' ? '' : this.time[1]
      this.searchForm.startTime = startTime
      this.searchForm.endTime = endTime
      const res = await getInstockListApi(this.searchForm)
      if (!res.success) return this.$notify.error(res.data.errorMsg)
      this.stockList = res.data.rows
      this.total = res.data.total
    },
    pageChange (val) {
      this.searchForm.pageNum = val
      this.getStockList()
    },
    pageSizeChange (val) {
      this.searchForm.pageSize = val
      this.getStockList()
    },
    handleReset () {
      this.searchForm = {
        pageNum: 1,
        pageSize: 5,
        type: '',
        status: 0,
        inNum: ''
      }
      this.time = []
      this.handleSearch()
    }
  }
}
</script>

<style lang='less' scoped>
.table_box {
  margin-top: 20px;
}
</style>
